package com.ruoyi.lqm.service;

import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgMaterialOptionalRequest;
import com.taobao.api.request.TbkDgOptimusMaterialRequest;
import com.taobao.api.request.TbkUatmFavoritesItemGetRequest;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import com.taobao.api.response.TbkUatmFavoritesItemGetResponse;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by DELL on 2019/9/16.
 */
@Service
public class AlimamaService {
    // TOP服务地址，正式环境
    public static final String url = "http://gw.api.taobao.com/router/rest";


    //创建应用时，TOP颁发的唯一标识，TOP通过App Key来鉴别应用的身份。调用接口时必须传入的参数。
    public static final String appkey = "25668654";

    //App Secret是TOP给应用分配的密钥，开发者需要妥善保存这个密钥，这个密钥用来保证应用来源的可靠性，防止被伪造。
    public static final String secret = "0bcaa7fa35da7dbd62a74173b03f5171";

    //淘宝联盟PID 第三位 adzone_id
    public static final long adzone_id = 99283600380L;

    /**
     * 物料精选
     */
    public List<TbkDgOptimusMaterialResponse.MapData> TbkDgOptimusMaterialRequest(Long pageno,Long materialId)
            throws ApiException {
        System.out.println("===================TbkDgOptimusMaterialRequest()=========================");
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
        req.setPageSize(20L);
        req.setAdzoneId(adzone_id);
        req.setPageNo(pageno);
        req.setMaterialId(materialId);
//        req.setDeviceValue("xxx");
//        req.setDeviceEncrypt("MD5");
//        req.setDeviceType("IMEI");
//        req.setContentId(323L);
//        req.setContentSource("xxx");
//        req.setItemId(33243L);
        TbkDgOptimusMaterialResponse rsp = client.execute(req);
        System.out.println("pageno==="+pageno);
        System.out.println("materialId==="+materialId);
        System.out.println(rsp.getBody());
        if(rsp.isSuccess()){
            return rsp.getResultList();
        }
        return null;
    }


    /**
     * 物料搜索
     * */
    public Object TbkDgMaterialOptionalRequest(Long pageno,String q,String sort) throws ApiException {
        System.out.println("===================TbkDgMaterialOptionalRequest()=========================");
        System.out.println(IpUtils.getIpAddr(ServletUtils.getRequest()));
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
        req.setPageSize(20L);
        req.setPageNo(pageno);
        req.setPlatform(1L);
        req.setEndTkRate(10000L); //佣金上下限
        req.setStartTkRate(1000L);
//        req.setEndPrice(10L);   //折扣上下限
//        req.setStartPrice(10L);
        req.setIsOverseas(false);
        req.setIsTmall(false);
        /*排序_des（降序），排序_asc（升序），销量（total_sales），
        淘客佣金比率（tk_rate）， 累计推广量（tk_total_sales），
        总支出佣金（tk_total_commi），价格（price）*/
        req.setSort(sort);
//        req.setItemloc("杭州");   //商品筛选-所在地
        req.setCat("");      //商品筛选-后台类目ID
        req.setQ(q);        // 商品筛选-查询词
        req.setMaterialId(17004L);      //物料ID
        req.setHasCoupon(false);    //优惠券筛选-是否有优惠券
//        req.setIp("13.2.33.4");     //ip参数影响邮费获取
        req.setAdzoneId(adzone_id);
        req.setNeedFreeShipment(true);
        req.setNeedPrepay(true);
        req.setIncludePayRate30(true);
        req.setIncludeGoodRate(true);
        req.setIncludeRfdRate(true);
        req.setNpxLevel(2L);
//        req.setEndKaTkRate(1234L);
//        req.setStartKaTkRate(1234L);
//        req.setLockRateEndTime(1567440000000L);
//        req.setLockRateStartTime(1567440000000L);
        TbkDgMaterialOptionalResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        if(rsp.isSuccess()){
            return rsp.getResultList();
        }
        return  null;
    }

    /**
     * 选品库宝贝信息
     * */
    public Object TbkUatmFavoritesItemGetRequest() throws ApiException {
        System.out.println("===================TbkUatmFavoritesItemGetRequest()=========================");
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkUatmFavoritesItemGetRequest req = new TbkUatmFavoritesItemGetRequest();
        req.setPlatform(1L);
        req.setPageSize(20L);
        req.setAdzoneId(adzone_id);
        req.setUnid("3456");
        req.setFavoritesId(16521L);
        req.setPageNo(2L);
        req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price," +
                "user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap," +
                "event_start_time,event_end_time,tk_rate,status,type");
        TbkUatmFavoritesItemGetResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        if(rsp.isSuccess()){
            return rsp.getResults();
        }
        return  null;
    }


}
